package com.heima.media.task;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class ArticleDelayTask {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private IWmNewsService wmNewsService;
    @Autowired
    private IAuditService auditService;

    @Async
    public void handleMessage() throws Exception {
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("news-dqueue");
        // 根据blockingDeque获取延迟队列
        RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);

        while (true) {  //死循环
            Object result = blockingDeque.poll(30, TimeUnit.SECONDS);
            if (result != null) {
             // result就是wmNews的id

                WmNews wmNews = wmNewsService.getById(Integer.parseInt(result.toString()));
                Long article = auditService.saveApArticle(wmNews); //需要把saveApArticle方法改成public
                wmNews.setArticleId(article);
                wmNews.setStatus(9);
                wmNewsService.updateById(wmNews);
                log.info("延迟任务发布成功。。。。。。。。");
            }
        }
    }
}
